﻿function AzureOnlineCreateBlobDetailsView(context) {

    var dispatcher = $({});
    var name = ko.observable('');
    var type = ko.observable('BlockBlob');
    var size = ko.observable(0);
    var instantType = ko.observable('BlockBlob');
    var content = ko.observable('');
    var triedToSubmit = ko.observable(false);
    var container = context.blobs.container;

    ko.computed(function () {
        instantType(type());
    });

    var validName = ko.computed(function () {
        if (!triedToSubmit()) return true;
        if (showNameRequired()) return false;
        if (showLength()) return false;
        return true;
    });

    var showNameRequired = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return true;
        return false;
    });

    var showLength = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        if (n.length > 1024) return true;
        return false;
    });

    function reset() {
        triedToSubmit(false);
        name('');
        setBlockType();
    }

    function setBlockType() {
        type('BlockBlob');
    }

    function setPageType() {
        type('PageBlob');
    }

    function validate() {
        triedToSubmit(true);
        return validName();
    }

    function create() {
        if (validate()) {
            var api = context.api();
            var cnt = container();
            var blob = name();
            var options = {
                container: cnt,
                blob: blob,
                type: type(),
                success: function () {
                    reset();
                    dispatcher.trigger('success');
                }
            };
            switch (type()) {
                case 'BlockBlob':
                    options.content = content();
                    break;
                case 'PageBlob':
                    options.size = size();
                    break;
            }
            api.blob.putBlob(options);
        }
    }

    this.container = container;
    this.name = name;
    this.type = type;
    this.size = size;
    this.content = content;
    this.instantType = instantType;
    this.setBlockType = setBlockType;
    this.setPageType = setPageType;

    this.setInstantBlockType = function () {
        instantType('BlockBlob');
    };
    this.setInstantPageType = function () {
        instantType('PageBlob');
    };

    this.validName = validName;
    this.hasErrors = ko.computed(function () {
        return !validName();
    });
    this.showNameRequired = showNameRequired;
    this.showLength = showLength;

    this.reset = reset;
    this.create = create;

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}